<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/

// Joomla security
defined('_JEXEC') or die;

class RequestHelper{

	public function request($srvid, $reqip, $reqsong){

		$sam = JooSamHelper::settings('sam', $srvid);
		//Build up configs
		$sam_ip = $sam['server'];
		$sam_port = $sam['port'];
		$sam_server = 'http://'.$sam_ip.':'.$sam_port.'/req/';
		$req_ip = urlencode($reqip);
		$song_id = (int)$reqsong;
		$txtcode = 0;
		$rid = 0;
		//Build the url for cURL
		$url = $sam_server.'?songID='.$song_id.'&host='.$req_ip;
		// Make request
		if(function_exists('curl_init')){
			$srv = curl_init($url);
			curl_setopt($srv, CURLOPT_HEADER, false);
			curl_setopt($srv, CURLOPT_RETURNTRANSFER, true);
			$response = curl_exec($srv);
			curl_close($srv);
			if($response == ''){
				$error = 800;				// Not possible to connect to sam server
			}
			else if($response == 'File not found'){
				$error = 801;				// Wrong SAM url given ip/port ar op but /req/?songID... is wrong.
			}
			else{
				$xml = simplexml_load_string($response);
				$txtcode = $xml->status->code;
				$rid = $xml->status->requestID;
				settype($txtcode, 'integer');
				settype($rid, 'integer');
				$error = 0;
			}
		}
		else{
			$error = 803;
		}
		if($txtcode == 0){$txtcode = $error;}
		$result = array('status' => 'valid', 'txtcode' => $txtcode, 'rid' => $rid );
		return $result;
	}
}